package com.yinhe.music.yhmusic.dialog.more;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.share.SharePresenter;

public class MoreDialogPresenter extends SharePresenter<BaseModel, IMoreDialogContract.IMoreDialogView> implements
        IMoreDialogContract.IMoreDialogPresenter {

    @Override
    public void getMusicInfo(Music music) {
        addSubscription(mModel.getMusicInfo((int) music.getSongId())
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(music1 -> {
                    getView().updateMusicInfoUI(music1);
                }, throwable -> {
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void vipBuyMusic(String product) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getPayInfo(IConstants.MEMBER, IConstants.PRODUCT_SONG, product)
                .compose(schedulerProvider.applySchedulers())
                .subscribe(payInfoResponse -> {
                    if (payInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
                        RxBus.get().post(RxBusEventType.Music.BUY_MUSIC, product);
                    }
                    getView().showToast(payInfoResponse.getMsg());
                    getView().setVipBuyUI();
                }, throwable -> {
                    getView().setVipBuyUI();
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void getMvInfo(int mvId) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getMvInfo(mvId)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(mvsInfo -> {
                    getView().updateMvInfoUI(mvsInfo);
                }, throwable -> {
                    getView().showMessage(throwable);
                }));
    }
}
